package gjbsh.bangong.ds.controller;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import gjbsh.bangong.ds.model.组织活动;
import gjbsh.bangong.ds.model.组织活动类型;
import gjbsh.bangong.ds.service.组织活动Service;
import gjbsh.bangong.ds.service.组织活动类型Service;
import gjbsh.support.BooleanMessage;
import gjbsh.support.JsonResult;
import gjbsh.support.StringHandle;


@RestController
@RequestMapping("zuzhihuodong")
public class 组织活动Controller {
	
	private Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired
	组织活动Service dd组织活动Service;
	
	@Autowired
	组织活动类型Service dd组织活动类型Service;
	

	// 查找一个
	@RequestMapping("find_one")
	public JsonResult<?> findOne(@RequestParam("huodongid")String ppHuodongId){
		try {
			if("".equals(ppHuodongId)) {
				return JsonResult.getErrorResult("组织活动ID不能为空");
			}
			return JsonResult.getSuccessResult(dd组织活动Service.findOne(ppHuodongId));
		} catch (Exception e) {
			
			logger.error("组织活动Controller->find_one: "+e.getMessage());
			return JsonResult.getErrorResult("组织活动查询失败");
		
		}
	}
	
	// 查询有效的活动
	@RequestMapping("find_valid")
	public JsonResult<?> findByValid(){
		try {		
			return JsonResult.getSuccessResult(dd组织活动Service.findValid());
		} catch (Exception e) {
			
			logger.error("组织活动Controller->find_valid: "+e.getMessage());
			return JsonResult.getErrorResult("查询有效的活动失败");
		}
	}
	
	// 查询所有的活动
	@RequestMapping("find_all")
	public JsonResult<?> findAll(){
		try {		
			return JsonResult.getSuccessResult(dd组织活动Service.findAll());
		} catch (Exception e) {
			
			logger.error("组织活动Controller->find_all: "+e.getMessage());
			return JsonResult.getErrorResult("查询所有组织活动失败");
		}
	}
	
   // 修改状态
	@RequestMapping("set_isvalid")
	public JsonResult<?> set_zhuangtai(@RequestParam("huodongid")String ppHuodongId,@RequestParam("isvalid")int ppIsvalid,
			HttpServletRequest request){
		try {
			if("".equals(ppHuodongId)) {
				return JsonResult.getErrorResult("活动ID不能为空");
			}
			组织活动 mm组织活动 = new 组织活动();
			mm组织活动.set组织活动id(ppHuodongId);
			mm组织活动.set是否有效(new BigDecimal(ppIsvalid));
			
			boolean Isok = dd组织活动Service.modify(mm组织活动);
			
			if(Isok) {
				return JsonResult.getSuccessResult("修改成功");
			}else {
				return JsonResult.getErrorResult("修改失败");
			}
			
		} catch (Exception e) {
			logger.error("组织活动Controller->set_isvalid: "+e.getMessage());
			return JsonResult.getErrorResult("修改组织活动状态失败");
		}
	}
	
	// 修改
	@RequestMapping("modify")
	public JsonResult<?> findBelongToOrganByDeptId(@RequestParam("huodongid")String ppHuodongId,
			@RequestParam("huodongleixingid")String ppHuodongleixingId,
			@RequestParam("huodongmingcheng")String ppHuodongmingcheng,
			@RequestParam("huodongdidian")String ppHuodongdidian,
			@RequestParam("huodongluxian")String ppHuodongLuxian,
			@RequestParam("danweirenshu")int ppDanweirenshu,
			@RequestParam("zongrenshu")int ppZongrenshu,		
			@RequestParam("baomingkaishishijian")String ppBaomingkaishishijian,
			@RequestParam("baomingjiezhishijian")String ppBaomingjiezhishijian,
			@RequestParam("huodongkaishishijian")String ppHuodongkaishishijian,
			@RequestParam("huodongjieshushijian")String ppHuodongjieshushijian,
			@RequestParam("qita")String ppQita){
		try {
			if("".equals(ppHuodongId)) {
				return JsonResult.getErrorResult("活动ID不能为空");
			}
			组织活动 mm组织活动 =dd组织活动Service.findOne(ppHuodongId);
			if(mm组织活动 == null) {
				return JsonResult.getErrorResult("该组织活动不存在");
			}
			
			// 进行数据的判断
			BooleanMessage mmBooleanMessage =checkShuju(ppHuodongleixingId, ppHuodongmingcheng, ppHuodongdidian, ppHuodongLuxian, ppDanweirenshu, ppZongrenshu, ppBaomingkaishishijian, ppBaomingjiezhishijian, ppHuodongkaishishijian, ppHuodongjieshushijian, ppQita);
			
			if(!mmBooleanMessage.isOk()) {
				return JsonResult.getErrorResult(mmBooleanMessage.getMessage().toString());
			}
			
			SimpleDateFormat mmSimpleDateFormat =new SimpleDateFormat("yyyy-MM-dd");
			
			mm组织活动.set活动类型(ppHuodongleixingId);
			mm组织活动.set活动名称(ppHuodongmingcheng);
			mm组织活动.set活动地点(ppHuodongdidian);
			mm组织活动.set活动路线(ppHuodongLuxian);
			mm组织活动.set每单位可报名人数(new BigDecimal(ppDanweirenshu));
			mm组织活动.set总人数(new BigDecimal(ppZongrenshu));
			mm组织活动.set报名起始时间(mmSimpleDateFormat.parse(ppBaomingkaishishijian));
			mm组织活动.set报名截止时间(mmSimpleDateFormat.parse(ppBaomingjiezhishijian));
			mm组织活动.set活动开始时间(mmSimpleDateFormat.parse(ppHuodongkaishishijian));
			mm组织活动.set活动结束时间(mmSimpleDateFormat.parse(ppHuodongjieshushijian));
			mm组织活动.set其他(ppQita);	
			
            boolean Isok = dd组织活动Service.modify(mm组织活动);
			
			if(Isok) {
				return JsonResult.getSuccessResult("修改成功");
			}else {
				return JsonResult.getErrorResult("修改失败");
			}
			
		} catch (Exception e) {
			logger.error("组织活动Controller->modify: "+e.getMessage());
			return JsonResult.getErrorResult("修改组织活动失败");
		}
	}
	
    // 添加
	@RequestMapping("add")
	public JsonResult<?> findCustomByDeptname(@RequestParam("huodongleixingid")String ppHuodongleixingId,
			@RequestParam("huodongmingcheng")String ppHuodongmingcheng,
			@RequestParam("huodongdidian")String ppHuodongdidian,
			@RequestParam("huodongluxian")String ppHuodongLuxian,
			@RequestParam("danweirenshu")int ppDanweirenshu,
			@RequestParam("zongrenshu")int ppZongrenshu,		
			@RequestParam("baomingkaishishijian")String ppBaomingkaishishijian,
			@RequestParam("baomingjiezhishijian")String ppBaomingjiezhishijian,
			@RequestParam("huodongkaishishijian")String ppHuodongkaishishijian,
			@RequestParam("huodongjieshushijian")String ppHuodongjieshushijian,
			@RequestParam("qita")String ppQita){
		try {
			// 进行数据的判断
			BooleanMessage mmBooleanMessage =checkShuju(ppHuodongleixingId, ppHuodongmingcheng, ppHuodongdidian, ppHuodongLuxian, ppDanweirenshu, ppZongrenshu, ppBaomingkaishishijian, ppBaomingjiezhishijian, ppHuodongkaishishijian, ppHuodongjieshushijian, ppQita);
			
			if(!mmBooleanMessage.isOk()) {
				return JsonResult.getErrorResult(mmBooleanMessage.getMessage().toString());
			}
			
			SimpleDateFormat mmSimpleDateFormat =new SimpleDateFormat("yyyy-MM-dd");
			组织活动 mm组织活动 = new 组织活动();
			mm组织活动.set组织活动id(UUID.randomUUID().toString());
			mm组织活动.set活动类型(ppHuodongleixingId);
			mm组织活动.set活动名称(ppHuodongmingcheng);
			mm组织活动.set活动地点(ppHuodongdidian);
			mm组织活动.set活动路线(ppHuodongLuxian);
			mm组织活动.set每单位可报名人数(new BigDecimal(ppDanweirenshu));
			mm组织活动.set总人数(new BigDecimal(ppZongrenshu));
			mm组织活动.set报名起始时间(mmSimpleDateFormat.parse(ppBaomingkaishishijian));
			mm组织活动.set报名截止时间(mmSimpleDateFormat.parse(ppBaomingjiezhishijian));
			mm组织活动.set活动开始时间(mmSimpleDateFormat.parse(ppHuodongkaishishijian));
			mm组织活动.set活动结束时间(mmSimpleDateFormat.parse(ppHuodongjieshushijian));
			mm组织活动.set排序号(new BigDecimal(dd组织活动Service.findMaxXuhao()+1));
			mm组织活动.set是否有效(new BigDecimal(1));
			mm组织活动.set其他(ppQita);	
            boolean Isok = dd组织活动Service.add(mm组织活动);
			
			if(Isok) {
				return JsonResult.getSuccessResult("添加成功");
			}else {
				return JsonResult.getErrorResult("添加失败");
			}
			
		} catch (Exception e) {
			logger.error("组织活动Controller->add: "+e.getMessage());
			return JsonResult.getErrorResult("添加组织活动失败");
		}
	}
	
	// 检查数据合法性
		private BooleanMessage checkShuju(
				String ppHuodongleixingId,
				String ppHuodongmingcheng,
				String ppHuodongdidian,
				String ppHuodongLuxian,
				int ppDanweirenshu,
				int ppZongrenshu,		
				String ppBaomingkaishishijian,
				String ppBaomingjiezhishijian,
				String ppHuodongkaishishijian,
				String ppHuodongjieshushijian,
				String ppQita)
				 throws ParseException {
			组织活动类型 mm组织活动类型 =dd组织活动类型Service.findOne(ppHuodongleixingId);
			
			if(mm组织活动类型 == null) {
				return BooleanMessage.getErrorMessage("该组织活动类型不存在");
			}
			
			if("".equals(ppHuodongmingcheng)) {
				return BooleanMessage.getErrorMessage("活动名称不能为空");
			}
			if(StringHandle.getStringByteLength(ppHuodongmingcheng) > 255) {
				return BooleanMessage.getErrorMessage("活动名称不可超过255字节");
			}
			
			if("".equals(ppHuodongdidian)) {
				return BooleanMessage.getErrorMessage("活动地点不能为空");
			}
			if(StringHandle.getStringByteLength(ppHuodongdidian) > 255) {
				return BooleanMessage.getErrorMessage("活动地点不可超过255字节");
			}
			
			if(StringHandle.getStringByteLength(ppHuodongLuxian) > 2000) {
				return BooleanMessage.getErrorMessage("活动路线不可超过2000字节");
			}
			
			
			if(StringHandle.getStringByteLength(ppQita) > 2000) {
				return BooleanMessage.getErrorMessage("其他不可超过2000字节");
			}
			
			
			return BooleanMessage.getSuccessMessage("数据合法");
		}

}
